<?php
// 获取维修员信息
// header("content-type:text/html;charset=utf-8");
include_once("conn.php");

if(empty($_GET['searchway'])){
    $sql = "SELECT * FROM tb_repairer ORDER BY r_id ASC";
}else{
    if($_GET['searchway'] == 'by_r_id_change'){
        $sql = "SELECT * FROM tb_repairer WHERE r_id = '".$_GET['r_id']."'";
    }
    if($_GET['searchway'] == 'by_r_id_del'){
        $sql = "SELECT * FROM tb_repairer WHERE r_id = '".$_GET['r_id']."'";
    }
}

$result = mysqli_query($conn_DB, $sql);
if($result){
    if(empty($_GET['searchway'])){
        if(mysqli_num_rows($result) <= 0){
            echo "<tr><td colspan='5'>暂无维修人员信息</td></tr>";
        }else{
            while($row = mysqli_fetch_object($result)){
                if($row->r_status == '0'){
                    $state = '停用';
                }
                if($row->r_status == '1'){
                    $state = '正常';
                }
                echo"
                    <tr>
                        <td>".$row->r_id."</td>
                        <td>".$row->r_name."</td>
                        <td>".$row->r_sex."</td>
                        <td>".$row->r_phone."</td>
                        <td>".$state."</td>
                    </tr>
                ";
            }
        }
    }else{
        if($_GET['searchway'] == 'by_r_id_change'){
            if(mysqli_num_rows($result) == 0){
                echo "<div class='none-info'><p>未查询到该维修员信息，请确认工号是否正确</p></div>";
            }else{
                $row = mysqli_fetch_object($result);
                if($row->r_sex == '男'){
                    $sex = "<option value='男' selected>男</option><option value='女'>女</option>";
                }else{
                    $sex = "<option value='男'>男</option><option value='女' selected>女</option>";
                }
                echo"
                <form class='row g-3' method='post' action='php/deal-tasks.php'>
                    <input type='hidden' name='action' value='change_repairer'>
                    <input type='hidden' name='r_id' value='".$row->r_id."'>
                    <div class='col-md-3'>
                        <label for='r_name' class='form-label'>维修员姓名</label>
                        <input type='text' class='form-control' id='r_name' name='r_name' value='".$row->r_name."' required>
                    </div>
                    <div class='col-md-4'>
                        <label for='r_phone' class='form-label'>维修员联系电话</label>
                        <input type='text' class='form-control' id='r_phone' name='r_phone' value='".$row->r_phone."' required>
                    </div>
                    <div class='col-md-2'>
                        <label for='r_sex' class='form-label'>维修员性别</label>
                        <select class='form-select' id='r_sex' name='r_sex' required>
                            ".$sex."
                        </select>
                    </div>
                    <div class='col-12 btn-style'>
                        <button type='submit' class='btn btn-primary' onclick='return checkInfo_change()'>确认修改</button>
                    </div>
                </form>
                ";
            }
        }
        if($_GET['searchway'] == 'by_r_id_del'){
            if(mysqli_num_rows($result) == 0){
                echo "<div class='none-info'><p>未查询到该维修员信息，请确认工号是否正确</p></div>";
            }
            else{
                $row = mysqli_fetch_object($result);
                if($row->r_status == '1'){
                    $s = "正常使用中";
                    $v = "del_repairer";
                    $status = "<div class='button-div'><button type='submit' class='btn btn-outline-danger'>停用该维修员账号</button>";
                }else{
                    $s = "账号停用中";
                    $v = "recover_repairer";
                    $status = "<div class='button-div'><button type='submit' class='btn btn-outline-primary'>启用该维修员账号</button></div>";
                }
                echo"
                    <form action='php/deal-tasks.php' method='post' class='form'>
                        <h3>员工基本信息</h3>
                        <input type='hidden' name='action' value='".$v."'>
                        <div class='mb-3'>
                            <label for='r_id' class='form-label'>维修员工号</label>
                            <input type='text' class='form-control' id='r_id' name='r_id' value='".$row->r_id."' readonly>
                        </div>
                        <div class='mb-3'>
                            <label for='r_name' class='form-label'>维修员姓名</label>
                            <input type='text' class='form-control' id='r_name' value='".$row->r_name."' readonly>
                        </div>
                        <div class='mb-3'>
                            <label for='r_sex' class='form-label'>维修员性别</label>
                            <input type='text' class='form-control' id='r_sex' value='".$row->r_sex."' readonly>
                        </div>
                        <div class='mb-3'>
                            <label for='r_phone' class='form-label'>维修员联系电话</label>o
                            <input type='text' class='form-control' id='r_phone' value='".$row->r_phone."' readonly>
                        </div>
                        <div class='mb-3'>
                            <label for='r_status' class='form-label'>账号状态</label>
                            <input type='text' class='form-control' id='r_status' value='".$s."' readonly>
                        </div>
                        ".$status."
                    </form>
                ";
            }
        }
    }
}